package com.morn.fanfou.db;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import android.annotation.SuppressLint;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

import com.morn.fanfou.model.BaseModel;

public class FanfouProvider extends ContentProvider {

	@SuppressLint("UseSparseArrays")
	public static final Map<Integer, BaseModel> map = new HashMap<Integer, BaseModel>();

	public static final UriMatcher mUriMatcher;

	private SQLiteOpenHelper openHelper;

	static {
		mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
	}

	@Override
	public boolean onCreate() {
		openHelper = new FanfouDatabaseHelper(this.getContext());
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		return ((BaseModel) map.get(Integer.valueOf(mUriMatcher.match(uri)))).query(uri, projection, selection,
				selectionArgs, sortOrder, openHelper, getContext());
	}

	@Override
	public String getType(Uri uri) {
		return ((BaseModel) map.get(Integer.valueOf(mUriMatcher.match(uri)))).getType(uri);
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		return ((BaseModel) map.get(Integer.valueOf(mUriMatcher.match(uri)))).insert(uri, values, openHelper,
				getContext());
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		return ((BaseModel) map.get(Integer.valueOf(mUriMatcher.match(uri)))).delete(uri, selection, selectionArgs,
				openHelper, getContext());
	}

	@Override
	public int bulkInsert(Uri uri, ContentValues[] values) {
		return ((BaseModel) map.get(Integer.valueOf(mUriMatcher.match(uri)))).bulkInsert(uri, values, openHelper,
				getContext());
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		return ((BaseModel) map.get(Integer.valueOf(mUriMatcher.match(uri)))).update(uri, values, selection,
				selectionArgs, openHelper, getContext());
	}

	class FanfouDatabaseHelper extends SQLiteOpenHelper {

		private static final String DATABASE_NAME = "fanfou.db";

		private static final int DATABASE_VERSION = 1;

		public FanfouDatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			Iterator<Entry<Integer, BaseModel>> localIterator = map.entrySet().iterator();
			while (localIterator.hasNext()) {
				db.execSQL(((BaseModel) ((Map.Entry<Integer, BaseModel>) localIterator.next()).getValue())
						.getCreateSql());
			}

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Iterator<Entry<Integer, BaseModel>> localIterator = map.entrySet().iterator();
			while (localIterator.hasNext()) {
				Map.Entry<Integer, BaseModel> localEntry = (Map.Entry<Integer, BaseModel>) localIterator.next();
				db.execSQL("DROP TABLE IF EXISTS " + ((BaseModel) localEntry.getValue()).tableName);
			}
			onCreate(db);
		}

	}
}
